package com.dongdong.zhuangji.utils;
import com.dongdong.zhuangji.entity.PileData;
import com.dongdong.zhuangji.service.PileDataService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.FileOutputStream;

@Component
public class ExcelExporter {

    @Autowired
    private PileDataService pileDataService;

    /**
     *  导出到excel
     * @param filePath
     * @throws IOException
     */
    public void exportToExcel(String filePath) throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("Pile Data");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {
                "序号", "桩基号", "里程", "偏距", "坐标X", "坐标Y",
                "桩底高程", "桩顶高程", "桩径", "桩长",
                "钢筋笼长度", "超灌长度", "地面高程", "实际桩底高程",
                "开挖时间", "开孔时间", "成孔时间",
                "下钢筋笼时间", "下导管时间", "浇筑时间", "试块编号"
        };
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 获取数据
        List<PileData> pileDataList = pileDataService.list();

        // 格式化日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 填充数据
        int rowNum = 1;
        for (PileData pileData : pileDataList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(pileData.getId());
            row.createCell(1).setCellValue(pileData.getPileNumber());
            row.createCell(2).setCellValue(pileData.getMileage());
            row.createCell(3).setCellValue(pileData.getOffset());
            row.createCell(4).setCellValue(pileData.getCoordinateX());
            row.createCell(5).setCellValue(pileData.getCoordinateY());
            row.createCell(6).setCellValue(pileData.getPileBottomElevation());
            row.createCell(7).setCellValue(pileData.getPileTopElevation());
            row.createCell(8).setCellValue(pileData.getPileDiameter());
            row.createCell(9).setCellValue(pileData.getPileLength());
            row.createCell(10).setCellValue(pileData.getSteelCageLength());
            row.createCell(11).setCellValue(pileData.getOverPouringLength());
            row.createCell(12).setCellValue(pileData.getGroundElevation());
            row.createCell(13).setCellValue(pileData.getActualPileBottomElevation());
            if (pileData.getExcavationTime() != null) {
                row.createCell(14).setCellValue(dateFormat.format(pileData.getExcavationTime()));
            }
            if (pileData.getHoleOpeningTime() != null) {
                row.createCell(15).setCellValue(dateFormat.format(pileData.getHoleOpeningTime()));
            }
            if (pileData.getHoleCompletionTime() != null) {
                row.createCell(16).setCellValue(dateFormat.format(pileData.getHoleCompletionTime()));
            }
            if (pileData.getReinforcementCageLoweringTime() != null) {
                row.createCell(17).setCellValue(dateFormat.format(pileData.getReinforcementCageLoweringTime()));
            }
            if (pileData.getConduitLoweringTime() != null) {
                row.createCell(18).setCellValue(dateFormat.format(pileData.getConduitLoweringTime()));
            }
            if (pileData.getPouringTime() != null) {
                row.createCell(19).setCellValue(dateFormat.format(pileData.getPouringTime()));
            }
            row.createCell(20).setCellValue(pileData.getTestBlockNumber());
        }

        // 调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // 保存文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
        } finally {
            workbook.close();
        }
    }

    }